package com.struts2.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

import com.struts2.model.Category;
import com.struts2.model.Color;
import com.struts2.model.Designer;
import com.struts2.model.Gender;
import com.struts2.model.Product_item;
import com.struts2.model.Product_line;
import com.struts2.model.Season;
import com.struts2.model.Size;
import com.struts2.model.Type;

public class ProductSearchService {

	public List<Product_line> GetProductList(List<Product_line> product_line,
			int productPerPage, int pageNum, String gender, String type,
			String category, int lower, int upper) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		//
		/*
		 * 
		 * Query query =
		 * session.createQuery("Select username FROM UserDetails"); List<String>
		 * userNames = (List<String>)query.list();
		 */
		// Query test = session.createQuery("FROM Product");

		Criteria criteria = session.createCriteria(Product_item.class,
				"product_item");
		criteria.createAlias("product_item.product_line", "product_line");

		if (!gender.equals("")) {
			// criteria.setFetchMode("gender", FetchMode.JOIN);
			criteria.createAlias("product_line.gender", "gender");
			criteria.add(Restrictions.eq("gender.name", gender));
		}
		if (!category.equals("")) {
			criteria.createAlias("product_line.category", "category");
			criteria.add(Restrictions.eq("category.name", category));
		}
		if (!type.equals("")) {
			criteria.createAlias("product_line.type", "type");
			criteria.add(Restrictions.eq("type.name", type));
		}
		criteria.add(Restrictions.eq("approval", 1));
		
		criteria.add(Restrictions.ge("product_line.price", lower));
		criteria.add(Restrictions.le("product_line.price", upper));
		
		// criteria.add(Restrictions.eq("user_id", 1));
		// criteria.add(Restrictions.sqlRestriction("LIMIT 0,2"));

	//	criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

		criteria.setProjection(Projections.distinct(Projections
				.projectionList()

				.add(Projections.property("product_line"))));

	
		
		
//		criteria.setProjection(Projections.projectionList().add(Projections.property("gender.id"),"id"));
		criteria.setFirstResult(productPerPage * (pageNum - 1));
		criteria.setMaxResults(productPerPage);
	//	criteria.setResultTransformer(Transformers.aliasToBean(Product_item.class));
	//	criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

		product_line = (List<Product_line>) criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();
		return product_line;
	}

	public int GetProductCount(List<Product_line> product_items, String gender,
			String type, String category, int lower, int upper) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		// for delete , update, insert,||| not select
		// session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_item.class,
				"product_item");
		criteria.createAlias("product_item.product_line", "product_line");

		if (!gender.equals("")) {
			// criteria.setFetchMode("gender", FetchMode.JOIN);
			criteria.createAlias("product_line.gender", "gender");
			criteria.add(Restrictions.eq("gender.name", gender));
		}
		if (!category.equals("")) {
			criteria.createAlias("product_line.category", "category");
			criteria.add(Restrictions.eq("category.name", category));
		}
		if (!type.equals("")) {
			criteria.createAlias("product_line.type", "type");
			criteria.add(Restrictions.eq("type.name", type));
		}
		
		criteria.add(Restrictions.eq("approval", 1));
		
		
		criteria.add(Restrictions.ge("product_line.price", lower));
		criteria.add(Restrictions.le("product_line.price", upper));
		
		criteria.setProjection(Projections.distinct(Projections
				.property("product_line")));

		
		
		int productCount = criteria.list().size();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return productCount;

	}

	public List<String> getLeftSideMenu(List<String> leftSideMenuList,
			String gender, String type, String category) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Category.class, "category");
		if (category.equals("Accessories")) {
			// criteria.createAlias("category.type", "type");
			criteria.add(Restrictions.eq("name", category));
			criteria.setProjection(Projections.property("name"));
			leftSideMenuList = (List<String>) criteria.list();
		} else {
			criteria = session.createCriteria(Gender.class, "gender");
			criteria.createAlias("gender.category", "category");
			criteria.add(Restrictions.eq("gender.name", gender));
			criteria.setProjection(Projections.property("category.name"));
			leftSideMenuList = (List<String>) criteria.list();
		}

		// getLeftSideMenu(leftSideMenuList, gender, type, category);
		// criteria.setProjection(Projections.distinct(Projections
		// .property("product")));

		session.getTransaction().commit();
		session.close();
		sF.close();
		return leftSideMenuList;
	}

	public List<String> getLeftSubSideMenu(List<String> leftSideSubMenuList,
			String gender, String type, String category) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		if (!gender.equals("")) {
			Criteria criteria = session.createCriteria(Gender.class, "gender");

			criteria.createAlias("gender.category", "category");

			criteria.createAlias("category.type", "type");
			criteria.add(Restrictions.eq("gender.name", gender));

			criteria.add(Restrictions.eq("category.name", category));
			criteria.setProjection(Projections.property("type.name"));
			leftSideSubMenuList = (List<String>) criteria.list();
			System.out.println("a" + gender + "n");
		} else {
			Criteria criteria = session.createCriteria(Category.class,
					"category");
			criteria.createAlias("category.type", "type");
			criteria.add(Restrictions.eq("category.name", category));
			criteria.setProjection(Projections.property("type.name"));
			leftSideSubMenuList = (List<String>) criteria.list();
			System.out.println("b");

		}

		// getLeftSideMenu(leftSubSideMenuList, gender, type, category);
		// criteria.setProjection(Projections.distinct(Projections
		// .property("product")));

		session.getTransaction().commit();
		session.close();
		sF.close();
		return leftSideSubMenuList;
	}
	
	
	
	
	@SuppressWarnings("unchecked")
	public List<Product_line> SearchProductList(List<Product_line>product_line , String input)
	{
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		
		Criteria criteria = session.createCriteria(Product_item.class,
				"product_item");
		criteria.createAlias("product_item.product_line", "product_line");
		
		criteria.add(Restrictions.like("product_line.name", "%"+input+"%"));
		
		criteria.add(Restrictions.eq("approval", 1));
		criteria.setProjection(Projections.distinct(Projections
				.projectionList()

				.add(Projections.property("product_line"))));

		
		product_line = (List<Product_line>) criteria.list();
		session.getTransaction().commit();
		session.close();
		sF.close();
		return product_line;
	}
}
